package com.wjk.kylin.mall.gateway.feign;

import cn.hutool.core.collection.CollectionUtil;
import com.wjk.kylin.mall.common.core.constant.Constants;
import com.wjk.kylin.mall.common.core.dto.admin.MenuComponentInDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * openfeign 统一异步线程调用
 * https://blog.csdn.net/niwo_345/article/details/123978446
 */
@Component
public class FeignHolder {

    @Autowired
    @Lazy
    private AdminFeignService adminFeignService;

    /**
     * 查询管理后台用户权限
     *
     * @param component
     * @return
     */
    @Async("taskExecutor")
    public Future<List<Long>> getRoleIdByComponent(String component) {
//        MDC.put(Constants.TRACE_ID_KEY,traceId);

        MenuComponentInDTO inDTO = new MenuComponentInDTO();
        inDTO.setComponent(component);
        DataOutDTO<List<Long>> outDTO = adminFeignService.getRoleIdByComponent(inDTO);
        if (outDTO == null || outDTO.getCode() != Constants.SUCCESS || CollectionUtil.isEmpty(outDTO.getData())) {
            new AsyncResult<>(new ArrayList<>());
        }
        return new AsyncResult<>(outDTO.getData());
    }

}
